package cn.ecut.controller;

import cn.ecut.model.*;
import cn.ecut.service.ShenqingdanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/shengqindan")
public class ShengqindanController {

    @Autowired
    private ShenqingdanService shenqingdanService;

    @ResponseBody
    @RequestMapping("/select")
    public HashMap selectShengqindan(HttpSession session) {
        Shenqingdan shenqingdan = new Shenqingdan();
        ShenqingdanMessage shenqingdanMessage = new ShenqingdanMessage();
        shenqingdan = shenqingdanService.selectAllByyuangongHao((String)(session.getAttribute("yuangongHao")));
        System.out.println(shenqingdan);
        shenqingdanMessage = shenqingdanService.Changeleixing(shenqingdan);
        System.out.println(shenqingdanMessage);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map = (HashMap)shenqingdanService.getShengqingdanMessqge(shenqingdanMessage);
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(HttpSession session , String yuangonghao){
        Message message = new Message();
        //String yuanggongHao=(String)session.getAttribute("yuangongHao");
        int n = shenqingdanService.deleteByYuangonghao(yuangonghao);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            message.setMsg("删除失败");
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Shenqingdan shenqingdan,String xiangmubianma,String taofangbianma,String loudongbianma,String fangjianbianma,String puweibianhao,HttpSession session) throws ParseException {
        Message message = new Message();
        String yuangonghao=(String)session.getAttribute("yuangongHao");
        Yuangong yuangong = new Yuangong();
        yuangong= shenqingdanService.selectYuangong(yuangonghao);
        Xiangmu xiangmu = new Xiangmu();
        xiangmu = shenqingdanService.selectXiangmu(xiangmubianma);
        Loudong loudong = new Loudong();
        loudong = shenqingdanService.selectLoudong(loudongbianma);
        Taofang taofang = new Taofang();
        taofang = shenqingdanService.selectTaofang(taofangbianma);
        Fangjian fangjian = new Fangjian();
        fangjian = shenqingdanService.selectFangjian(fangjianbianma);
        Puwei puwei = new Puwei();
        puwei = shenqingdanService.selectPuwei(puweibianhao);
        shenqingdan.setDanjvbianhao(shenqingdanService.getdanjvBianhao());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date1 = new Date();
        String s = simpleDateFormat.format(date1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date2 = sf.parse(s);
        shenqingdan.setDanjushenqingriqi(date2);
        shenqingdan.setShenqingren(yuangong.getXinming());
        shenqingdan.setYuangonghao(yuangonghao);
        shenqingdan.setXinbie(yuangong.getXinbie());
        shenqingdan.setSuozaibumem(yuangong.getSuozaiBumem());
        shenqingdan.setDanjuzhuangtai(0);
        shenqingdan.setShouji(yuangong.getShouji());
        shenqingdan.setGongzuodianhua(yuangong.getGongzuoDianhua());
        shenqingdan.setXueli(yuangong.getXueli());
        shenqingdan.setRuhangshijian(yuangong.getRuhangShijian());
        shenqingdan.setShengfenzhenghao(yuangong.getShengfenZhenghao());
        shenqingdan.setYinhangkahao(yuangong.getYinhangKahao());
        shenqingdan.setDizhi(yuangong.getDizhi());
        shenqingdan.setQuyu(fangjian.getQuyu());
        shenqingdan.setXiangmubianma(xiangmubianma);
        shenqingdan.setLoudongmingcheng(loudong.getLoudong());
        shenqingdan.setLouceng(fangjian.getLouceng());
        shenqingdan.setTaofang(taofang.getTaofangmingcheng());
        shenqingdan.setFangjian(fangjian.getFangjian());
        shenqingdan.setPuweibianhao(puweibianhao);
        shenqingdan.setPuweimingcheng(puwei.getPuwei());
        int n = shenqingdanService.insert(shenqingdan);
        if(n == 1){
            puwei.setShiyongzhuangtai(2);
            shenqingdanService.updatePuweiShiyongzhuangtai(puwei);
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/show")
    public HashMap show(){
        HashMap<String, Object> map = new HashMap<String, Object> ();
        List<Shenqingdan> list = new ArrayList<>();
        list = shenqingdanService.selectAllshenqingdan();
        Integer count = shenqingdanService.selectAllcount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/select2")
    public HashMap<String, Object> select2(String searchResult){
        HashMap<String, Object> map = new HashMap<String, Object> ();
        List<Shenqingdan> list = new ArrayList<>();
        list = shenqingdanService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/pass")
    public Message pass(Shenqingdan shenqingdan){
        Message message = new Message();
        Puwei puwei = new Puwei();
        puwei.setPuweibianhao(shenqingdan.getPuweibianhao());
        shenqingdan.setDanjuzhuangtai(1);
        int n = shenqingdanService.updateDanjuzhuangtai(shenqingdan);
        if(n == 1){
            puwei.setShiyongzhuangtai(3);
            shenqingdanService.updatePuweiShiyongzhuangtai(puwei);
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/fail")
    public Message fail(Shenqingdan shenqingdan){
        Message message = new Message();
        Puwei puwei = new Puwei();
        puwei.setPuweibianhao(shenqingdan.getPuweibianhao());
        shenqingdan.setDanjuzhuangtai(2);
        int n = shenqingdanService.updateDanjuzhuangtai(shenqingdan);
        if(n == 1){
            puwei.setShiyongzhuangtai(1);
            shenqingdanService.updatePuweiShiyongzhuangtai(puwei);
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/delete2")
    public Message delete2(String danjvbianhao,int danjuzhuangtai){
        Message message = new Message();
        if(danjuzhuangtai == 0){
            message.setFlag(false);
            message.setData("未审批，不能删除");
            return message;
        }else {
            int n = shenqingdanService.deleteByPrimaryKey(danjvbianhao);
            if(n == 1){
                message.setFlag(true);
                return message;
            }else {
                message.setFlag(false);
                message.setData("从数据库删除失败");
                return message;
            }
        }
    }
}




